package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerUpdateHistoryDAO;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.service.CustomerUpdateHistoryService;
import com.feeyin.radar.sys.dao.UserDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CustomerUpdateHistoryServiceImpl
  implements CustomerUpdateHistoryService
{
  private CustomerUpdateHistoryDAO customerUpdateHistoryDAO;
  private UserDAO userDAO;
  private CustomerDAO customerDAO;
  private SaleRecordDAO saleRecordDAO;
  private LinkmanDAO linkmanDAO;

  public CustomerUpdateHistoryDAO getCustomerUpdateHistoryDAO()
  {
    return this.customerUpdateHistoryDAO;
  }

  public void setCustomerUpdateHistoryDAO(CustomerUpdateHistoryDAO customerUpdateHistoryDAO)
  {
    this.customerUpdateHistoryDAO = customerUpdateHistoryDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }

  public List getPageCustomerUpdateHistorysByCustomerId(Integer customerId, Page page) {
    List list = new ArrayList();
    list = this.customerUpdateHistoryDAO.getPageCustomerUpdateHistorysByCustomerId(customerId, page);
    if (list == null) return null;
    for (int i = 0; i < list.size(); i++) {
      Customer customer = new Customer();
      customer = (Customer)list.get(i);
      customer.setUpdatePerson(this.userDAO.getUserNameById(customer.getUpdatePersonId()));

      List linkmans = new ArrayList();
      linkmans = this.linkmanDAO.getLinkmansByCustomerId(customer.getId());
      if ((linkmans == null) || (linkmans.size() <= 0))
        customer.setLinkmanNum(Integer.valueOf(0));
      else {
        customer.setLinkmanNum(Integer.valueOf(linkmans.size()));
      }

      customer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(customer.getId()));
    }
    return list;
  }

  public Integer getPageCustomerUpdateHistorysCountByCustomerId(Integer customerId)
  {
    return this.customerUpdateHistoryDAO.getPageCustomerUpdateHistorysCountByCustomerId(customerId);
  }

  public Customer getCustomerUpdateHistory(Integer id) {
    if (id == null) return null;
    Customer customer = new Customer();
    customer = this.customerUpdateHistoryDAO.getCustomerUpdateHistory(id);

    if (customer == null) return null;
    customer.setSalePerson(this.userDAO.getUserNameById(customer.getSalePersonId()));
    customer.setShareSalePersons(this.userDAO.getShareUsersByCustomerId(customer.getCustomerId(), customer.getSalePersonId()));

    return customer;
  }

  public boolean restoreCustomerInfo(Integer id, Integer currentUserId) {
    if ((id == null) || (currentUserId == null)) return false;
    Customer restoreCustomer = new Customer();
    restoreCustomer = this.customerUpdateHistoryDAO.getCustomerUpdateHistory(id);

    if (restoreCustomer == null) return false;
    restoreCustomer.setId(restoreCustomer.getCustomerId());
    restoreCustomer.setRestorePersonId(currentUserId);
    restoreCustomer.setRestoreDate(new Date());
    return this.customerDAO.updateCustomerByStaticParam(restoreCustomer);
  }
}